package com.ruoyi.training.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.training.domain.Remarks;
import com.ruoyi.training.service.IRecordsService;
import com.ruoyi.training.service.IRemarksService;
import com.ruoyi.training.util.xuehua.SnowflakeIdUtils;
import com.ruoyi.training.vo.CheckinDetailsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.training.mapper.CheckinRecordMapper;
import com.ruoyi.training.domain.CheckinRecord;
import com.ruoyi.training.service.ICheckinRecordService;

/**
 * 班级签到记录（学员的实际签到信息）Service业务层处理
 *
 * @author WorrilessGo
 * @date 2025-09-24
 */
@Service
public class CheckinRecordServiceImpl implements ICheckinRecordService {

    private static final SnowflakeIdUtils snowflake = new SnowflakeIdUtils(5, 8);

    @Autowired
    private CheckinRecordMapper checkinRecordMapper;

    @Autowired
    private IRemarksService remarksService;

    /**
     * 查询班级签到记录（学员的实际签到信息）
     *
     * @param checkinRecordId 班级签到记录（学员的实际签到信息）主键
     * @return 班级签到记录（学员的实际签到信息）
     */
    @Override
    public CheckinRecord selectCheckinRecordByCheckinRecordId(String checkinRecordId) {
        return checkinRecordMapper.selectCheckinRecordByCheckinRecordId(checkinRecordId);
    }

    /**
     * 查询班级签到记录（学员的实际签到信息）列表
     *
     * @param checkinRecord 班级签到记录（学员的实际签到信息）
     * @return 班级签到记录（学员的实际签到信息）
     */
    @Override
    public List<CheckinRecord> selectCheckinRecordList(CheckinRecord checkinRecord) {
        return checkinRecordMapper.selectCheckinRecordList(checkinRecord);
    }

    /**
     * 新增班级签到记录（学员的实际签到信息）
     *
     * @param checkinRecord 班级签到记录（学员的实际签到信息）
     * @return 结果
     */
    @Override
    public int insertCheckinRecord(CheckinRecord checkinRecord) {
        checkinRecord.setCreateTime(DateUtils.getNowDate());
        return checkinRecordMapper.insertCheckinRecord(checkinRecord);
    }

    /**
     * 修改班级签到记录（学员的实际签到信息）
     *
     * @param checkinRecord 班级签到记录（学员的实际签到信息）
     * @return 结果
     */
    @Override
    public int updateCheckinRecord(CheckinRecord checkinRecord) {
        checkinRecord.setUpdateTime(DateUtils.getNowDate());
        return checkinRecordMapper.updateCheckinRecord(checkinRecord);
    }

    /**
     * 批量删除班级签到记录（学员的实际签到信息）
     *
     * @param checkinRecordIds 需要删除的班级签到记录（学员的实际签到信息）主键
     * @return 结果
     */
    @Override
    public int deleteCheckinRecordByCheckinRecordIds(String[] checkinRecordIds) {
        return checkinRecordMapper.deleteCheckinRecordByCheckinRecordIds(checkinRecordIds);
    }

    /**
     * 删除班级签到记录（学员的实际签到信息）信息
     *
     * @param checkinRecordId 班级签到记录（学员的实际签到信息）主键
     * @return 结果
     */
    @Override
    public int deleteCheckinRecordByCheckinRecordId(String checkinRecordId) {
        return checkinRecordMapper.deleteCheckinRecordByCheckinRecordId(checkinRecordId);
    }

    /**
     * 批量修改签到状态
     * lqx
     */
    @Override
    public int batchUpdate(List<CheckinDetailsResult> checkinDetailsResultList) {
        int result = 0;
        for (CheckinDetailsResult checkinDetailsResult : checkinDetailsResultList) {
            //情况一：checkinRecordId不为空，直接通过checkinRecordId修改checkinStatus
            if (checkinDetailsResult.getCheckinRecordId() != null && !"".equals(checkinDetailsResult.getCheckinRecordId())) {
                CheckinRecord checkinRecord = new CheckinRecord();
                //赋值id
                checkinRecord.setCheckinRecordId(checkinDetailsResult.getCheckinRecordId());
                //赋值状态
                if (checkinDetailsResult.getCheckinStatus() != null) {
                    checkinRecord.setCheckinStatus(Long.valueOf(checkinDetailsResult.getCheckinStatus()));
                }
                //赋值修改时间
                checkinRecord.setUpdateTime(new Date());
                //赋值修改人
                checkinRecord.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
                //修改
                result = checkinRecordMapper.updateCheckinRecord(checkinRecord);
            } else {
                //情况二：checkinRecordId为空，新增一条签到记录，并匹配对应的checkinStatus
                CheckinRecord checkinRecord = addCheckinRecord(checkinDetailsResult);
                //新增
                result = checkinRecordMapper.insertCheckinRecord(checkinRecord);
            }
        }
        return result;
    }

    /**
     * 新增班级签到记录备注
     * lqx
     */
    @Override
    public int addCheckinRemark(CheckinDetailsResult checkinDetailsResult) {
        int result = 0;
        //情况一：checkinRecordId不为空，直接通过checkinRecordId添加备注
        if (checkinDetailsResult.getCheckinRecordId() != null && !"".equals(checkinDetailsResult.getCheckinRecordId())) {
            CheckinRecord checkinRecord = new CheckinRecord();
            //赋值id
            checkinRecord.setCheckinRecordId(checkinDetailsResult.getCheckinRecordId());
            //赋值备注
            if (checkinDetailsResult.getCheckinRemark() != null) {
                checkinRecord.setCheckinRemark(checkinDetailsResult.getCheckinRemark());
            }
            result = checkinRecordMapper.updateCheckinRecord(checkinRecord);
            //调用备注方法
            Remarks remarks = new Remarks();
            //设置外键id
            remarks.setForeignId(checkinDetailsResult.getCheckinRecordId());
            //设置备注类型
            remarks.setRemarkType(checkinDetailsResult.getRemarkType());
            //设置备注内容
            remarks.setRemarks(checkinDetailsResult.getCheckinRemark());
            remarksService.insertRemarks(remarks);
        } else {
            //情况二：checkinRecordId为空，新增一条签到记录，并匹配对应的checkinStatus
            CheckinRecord checkinRecord = addCheckinRecord(checkinDetailsResult);
            result = checkinRecordMapper.insertCheckinRecord(checkinRecord);
            //调用备注方法
            Remarks remarks = new Remarks();
            //设置外键id
            remarks.setForeignId(checkinRecord.getCheckinRecordId());
            //设置备注类型
            remarks.setRemarkType(checkinDetailsResult.getRemarkType());
            //设置备注内容
            remarks.setRemarks(checkinDetailsResult.getCheckinRemark());
            remarksService.insertRemarks(remarks);
        }
        return result;
    }

    //赋值CheckinRecord
    public CheckinRecord addCheckinRecord(CheckinDetailsResult checkinDetailsResult){
        CheckinRecord checkinRecord = new CheckinRecord();
        //生成id
        checkinRecord.setCheckinRecordId(String.valueOf(snowflake.nextId()));
        //设置班级签到表
        if (checkinDetailsResult.getClassCheckinId() != null && !"".equals(checkinDetailsResult.getClassCheckinId())) {
            checkinRecord.setClassCheckinId(checkinDetailsResult.getClassCheckinId());
        }
        //设置签到状态
        if (checkinDetailsResult.getCheckinStatus() != null) {
            checkinRecord.setCheckinStatus(Long.valueOf(checkinDetailsResult.getCheckinStatus()));
        }
        //设置匹配到的学员id
        if (checkinDetailsResult.getStudentId() != null && !"".equals(checkinDetailsResult.getStudentId())) {
            checkinRecord.setStudentId(checkinDetailsResult.getStudentId());
        }
        //设置学员姓名
        if (checkinDetailsResult.getStudentName() != null && !"".equals(checkinDetailsResult.getStudentName())) {
            checkinRecord.setStudentName(checkinDetailsResult.getStudentName());
        }
        //设置证件编号
        if (checkinDetailsResult.getIdNumber() != null && !"".equals(checkinDetailsResult.getIdNumber())) {
            checkinRecord.setIdCardNo(checkinDetailsResult.getIdNumber());
        }
        //设置电话
        if (checkinDetailsResult.getPhoneNumber() != null && !"".equals(checkinDetailsResult.getPhoneNumber())) {
            checkinRecord.setPhoneNumber(checkinDetailsResult.getPhoneNumber());
        }
        //设置备注
        if (checkinDetailsResult.getCheckinRemark() != null && !"".equals(checkinDetailsResult.getCheckinRemark())) {
            checkinRecord.setCheckinRemark(checkinDetailsResult.getCheckinRemark());
        }
        //设置创建时间
        checkinRecord.setCreateTime(new Date());
        //设置创建人
        checkinRecord.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        return checkinRecord;
    }
}
